package com.yanyu.space.sys.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yanyu.space.common.core.bean.db.PageEntity;
import com.yanyu.space.common.core.bean.web.R;
import com.yanyu.space.sys.bean.po.Dic;
import com.yanyu.space.sys.bean.vo.dic.DicSearchVo;
import com.yanyu.space.sys.service.IDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典 前端控制器
 *
 * @author yanyu
 */
@AllArgsConstructor
@Api(tags = "系统字典")
@RestController
@RequestMapping("/sys/dic")
@Validated
public class DicController {

    private IDicService dicService;

    @GetMapping("/list")
    @ApiOperation("查询字典列表")
    public R<PageEntity<Dic>> list(DicSearchVo vo) {
        return R.ok(dicService.listPage(vo));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询数据字典")
    public R<Dic> getById(@PathVariable("id") Long id) {
        return R.ok(dicService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增数据字典")
    public R add(@RequestBody Dic dic) {
        dicService.save(dic);
        return R.ok();
    }

    @PutMapping
    @ApiOperation("修改数据字典")
    public R udpateById(@RequestBody Dic dic) {
        dicService.update(dic);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除数组字典")
    public R deleteById(@PathVariable("id") Long id) {
        dicService.delete(id);
        return R.ok();
    }

    @GetMapping("/optionselect")
    @ApiOperation("查询数据字典")
    public R<List<Dic>> optionselect() {
        return R.ok(dicService.list(Wrappers.<Dic>lambdaQuery().orderByAsc(Dic::getSort)));
    }

}

